/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB)
    {
        int lenA = 0;
        int lenB = 0;
        ListNode* newA = headA, * newB = headB;
        while (headA)
        {
            lenA++;
            headA = headA->next;
        }
        while (headB)
        {
            lenB++;
            headB = headB->next;
        }
        int k = abs(lenA - lenB);
        if (lenA > lenB)
        {
            while (k--)
            {
                newA = newA->next;
            }
        }
        else
        {
            while (k--)
            {
                newB = newB->next;
            }
        }

        while (newA != newB)
        {
            newA = newA->next;

            newB = newB->next;
        }
        return newA;
    }
};